package com.wang.controller;


import com.wang.base.http.RestResponse;
import com.wang.domain.LargeCategory;
import com.wang.domain.SmallCategory;
import com.wang.model.Medicine.AddSmallCategoryVM;
import com.wang.model.Medicine.TagResponseVM;
import com.wang.service.LargeCategoryService;
import com.wang.service.MedicineService;
import com.wang.service.SmallCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author WangZai
 * @since 2023-05-06
 */
@CrossOrigin
@RestController
@RequestMapping("/td/api/smallCategory")
@Api(tags = "小类模块接口说明")
public class SmallCategoryController {
    private final ModelMapper modelMapper;
    private final MedicineService medicineService;
    private final LargeCategoryService largeCategoryService;
    private final SmallCategoryService smallCategoryService;

    @Autowired
    public SmallCategoryController(ModelMapper modelMapper, MedicineService medicineService, LargeCategoryService largeCategoryService, SmallCategoryService smallCategoryService) {
        this.modelMapper = modelMapper;
        this.medicineService = medicineService;
        this.largeCategoryService = largeCategoryService;
        this.smallCategoryService = smallCategoryService;
    }

    @Transactional(rollbackFor = Exception.class)
    @PostMapping(value = "/getData")
    @ApiOperation("管理员通过大类id获取小类接口说明")
    public RestResponse<Object> getData(@RequestParam("largeCategoryId") Integer largeCategoryId) {
        List<SmallCategory> smallCategoryList = smallCategoryService.getSmallCategoryListByLargeCategoryId(largeCategoryId);
        if (smallCategoryList.isEmpty()) {
            return RestResponse.failure("该大类无小类标签！");
        } else {
            return RestResponse.ok(smallCategoryList);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @PostMapping(value = "/addData")
    @ApiOperation("管理员添加小类数据接口说明")
    public RestResponse<Object> addData(@RequestBody AddSmallCategoryVM model) {
        SmallCategory smallCategory =modelMapper.map(model,SmallCategory.class);
        if (smallCategoryService.getSmallCategoryByName(model.getSmallCategoryName())==null) {
            smallCategoryService.addSmallCategory(smallCategory);
            return RestResponse.ok("添加成功！");
        } else {
            return RestResponse.failure("信息重复，添加失败！！！");
        }
    }


    @Transactional(rollbackFor = Exception.class)
    @GetMapping(value = "/deleteData/{smallCategoryId}")
    @ApiOperation("管理员删除小类及下属数据接口说明")
    public RestResponse<Object> deleteData(@PathVariable Integer smallCategoryId) {
        if (smallCategoryService.deleteSmallCategoryById(smallCategoryId)) {
            medicineService.deleteMedicineBySmallCategoryId(smallCategoryId);
            return RestResponse.ok("删除成功！");
        } else {
            return RestResponse.failure("删除失败！！！");
        }
    }


    @Transactional(rollbackFor = Exception.class)
    @GetMapping(value = "/getTags")
    @ApiOperation("管理员获取所有嵌套标签接口说明")
    public RestResponse<Object> getTags() {
        List<LargeCategory> largeCategoryList = largeCategoryService.getLargeCategories();

        List<TagResponseVM> tagResponseVMList = new ArrayList<>();
        if (largeCategoryList.isEmpty()){
            return RestResponse.failure("无相关数据！");
        }
        for(LargeCategory largeCategory:largeCategoryList){
            TagResponseVM tagResponseVM = new TagResponseVM();
            List<SmallCategory> smallCategoryList = smallCategoryService.getSmallCategoryListByLargeCategoryId(largeCategory.getLargeCategoryId());
            tagResponseVM.setSmallCategoryList(smallCategoryList);
            tagResponseVM.setLargeCategoryName(largeCategory.getLargeCategoryName());
            tagResponseVMList.add(tagResponseVM);
        }
        return RestResponse.ok(tagResponseVMList);
    }
}

